package com.ebupt.migu.music.common.util;

import com.google.common.io.Files;
import org.apache.commons.io.Charsets;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @description: TODO
 * @author: liuyangyang@ebupt.com
 * @date: 2021/4/27 15:30
 */
public class CompareFilesLineFunc {
    public static void main(String[] args) throws IOException {
        String path = "D:\\bugs\\";
        String sourceFileName = "zhangyuan_20210506_canyuyonghu.csv";
        List<String> sources = Files.readLines(new File(path + sourceFileName), Charsets.UTF_8);
        for (String source : sources) {
            source = source.replace("|", "&");
            String[] datas = source.split("&");
            String sid = datas[0].substring(0,16);
            String date = datas[0].substring(16);
            String success = datas[1];
            String read = datas[2];
            String unread = datas[3];
            StringBuffer str = new StringBuffer();
            str.append("update statistics_msg set success_num = ");
            str.append(success);
            str.append(", single_read = ");
            str.append(read);
            str.append(", single_unread = ");
            str.append(unread);
            str.append(", conversion_rate = ");
            str.append("'");
            str.append(conversion_rate(read, success));
            str.append("'");
            str.append(" where strategy_id = ");
            str.append("'");
            str.append(sid);
            str.append("'");
            str.append("\\g");
            System.out.println(str);
        }
    }

    public static String conversion_rate(String read, String success) {
        DecimalFormat df=new DecimalFormat("0.00");
        int a = Integer.parseInt(read);
        int b = Integer.parseInt(success);
        return df.format((float)a/b*100)+"%";
    }
}
